package com.base.vistter.iframe.entity;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@TableName(value = "sys_user")
public class SysUser extends BaseEntity implements UserDetails  {

    @TableField(updateStrategy = FieldStrategy.NEVER)
    private String username;

    private String realname;

    private String password;

    private String workNo;

    private String avatar;

    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd")
    private Date birthday;

    private Integer sex;

    private String email;

    private String phone;

    @TableField(updateStrategy = FieldStrategy.NEVER)
    private Integer isAdmin;

    private Integer status;

    private Integer sysJobId;

    private Integer sysRoleId;

    @TableField(exist = false)
    private String sysDepartName;

    @TableField(exist = false)
    private String sysRoleName;

    @TableField(exist = false)
    private String sysJobName;

    /**
     * 用户的账户是否未过期
     * @return true：未过期，false：已过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 用户的账户是否未被锁定
     * @return true：未锁定，false：已锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return this.status  == 0;
    }

    /**
     * 用户的凭证是否未过期
     * @return true：未过期，false：已过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    /**
     * 用户是否启用
     * @return true：启用，false：未启用
     */
    @Override
    public boolean isEnabled() {
        return this.status == 0;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String toString() {
        return "SysUser{" +
                "username='" + username + '\'' +
                ", realname='" + realname + '\'' +
                ", password='" + password + '\'' +
                ", workNo='" + workNo + '\'' +
                ", avatar='" + avatar + '\'' +
                ", birthday=" + birthday +
                ", sex=" + sex +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", status=" + status +
                ", sysJobId=" + sysJobId +
                ", sysRoleId=" + sysRoleId +
                ", sysDepartName='" + sysDepartName + '\'' +
                ", sysRoleName='" + sysRoleName + '\'' +
                ", sysJobName='" + sysJobName + '\'' +
                '}';
    }
}
